<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>sdk/keys.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="accountsApi.html">accountsApi</a><ul class='methods'><li data-type='method'><a href="accountsApi.html#create">create</a></li><li data-type='method'><a href="accountsApi.html#createAddress">createAddress</a></li><li data-type='method'><a href="accountsApi.html#listAddresses">listAddresses</a></li></ul></li><li><a href="queryApi.html">queryApi</a><ul class='methods'><li data-type='method'><a href="queryApi.html#getblockstatus">getblockstatus</a></li><li data-type='method'><a href="queryApi.html#listAsset">listAsset</a></li><li data-type='method'><a href="queryApi.html#listUTXO">listUTXO</a></li></ul></li><li><a href="transactionsApi.html">transactionsApi</a><ul class='methods'><li data-type='method'><a href="transactionsApi.html#buildPayment">buildPayment</a></li><li data-type='method'><a href="transactionsApi.html#buildTransaction">buildTransaction</a></li><li data-type='method'><a href="transactionsApi.html#convertArguement">convertArguement</a></li><li data-type='method'><a href="transactionsApi.html#list">list</a></li><li data-type='method'><a href="transactionsApi.html#submit">submit</a></li></ul></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">sdk/keys.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>import {createKey, resetKeyPassword, createPubkey} from '../wasm/func';
import {getDB} from '../db/db';


function keysSDK() {
}

/**
 * reset key password
 * 
 * @param {String}} rootXPub 
 * @param {String} oldPassword 
 * @param {String} newPassword 
 */
keysSDK.prototype.resetKeyPassword = function(rootXPub, oldPassword, newPassword) {
    let retPromise = new Promise((resolve, reject) => {
        let data = {rootXPub: rootXPub, oldPassword:oldPassword, newPassword:newPassword};
        resetKeyPassword(data).then(res => {
            getDB().then(db => {
                let objectStore = db.transaction(['keys'], 'readwrite').objectStore('keys');
                let index = objectStore.index('xpub');
                let keyRange = IDBKeyRange.only(rootXPub);
                let getRequest = index.openCursor(keyRange);
                getRequest.onsuccess = function (event) {
                    const cursor = event.target.result;
                    if(cursor &amp;&amp; cursor.value.xpub === rootXPub) {
                        const updateData = cursor.value;
                        updateData.key = res.data;
                        const request = cursor.update(updateData);
                        request.onsuccess = function() {
                            resolve(true);
                        };
                        request.onerror = function() {
                            reject(new Error('db update error'));
                        };
                    } else {
                        reject(new Error('db update error: not found by rootXPub'));
                    }
                };
                getRequest.onerror = function () {
                    reject(new Error('db get error'));
                };
            }).catch(error => {
                reject(error);
            });
        }).catch(error => {
            reject(error);
        });
    });
    return retPromise;
};

/**
 * get key by XPub
 * 
 * @param {String} xpub 
 */
keysSDK.prototype.getKeyByXPub = function(xpub) {
    let retPromise = new Promise((resolve, reject) => {
        getDB().then(db => {
            let getRequest = db.transaction(['keys'], 'readonly')
                .objectStore('keys')
                .index('xpub')
                .get(xpub);
            getRequest.onsuccess = function(e) {
                if(e.target.result) {
                    resolve(e.target.result.key);
                } else {
                    reject(new Error('not found by XPub'));    
                }
            };
            getRequest.onerror = function() {
                reject(new Error('db get error'));
            };
        }).catch(error => {
            reject(error);
        });
    });
    return retPromise;
};

/**
 * List key
 *
 * @returns {Promise}
 */
keysSDK.prototype.list = function() {
    let retPromise = new Promise((resolve, reject) => {
        getDB().then(db => {
            let transaction = db.transaction(['keys'], 'readonly');
            let objectStore = transaction.objectStore('keys');
            let oc = objectStore.openCursor();
            let ret = [];
            oc.onsuccess = function (event) {
                var cursor = event.target.result;
                if (cursor) {
                    ret.push({alias: cursor.value.alias, xpub: cursor.value.xpub});
                    cursor.continue();
                } else {
                    resolve(ret);
                }
            };
            oc.onerror = function(e){
                reject(e);
            };
        }).catch(err => {
            reject(err);
        });
    });
    return retPromise;
};

/**
 * Create a new key.
 * 
 * @param {String} alias - User specified, unique identifier.
 * @param {String} password - User specified, key password.
 */
keysSDK.prototype.create = function(alias, password) {
    var normalizedAlias = alias.toLowerCase().trim();
    let retPromise = new Promise((resolve, reject) => {
        getDB().then(db => {
            let getRequest = db.transaction(['keys'], 'readonly')
                .objectStore('keys')
                .index('alias')
                .get(normalizedAlias);
            getRequest.onsuccess = function (e) {
                if (e.target.result) {
                    reject(new Error('key alias already exists'));
                    return;
                }
                let data = {};
                data.alias = normalizedAlias;
                data.auth = password;
                createKey(data).then((res) => {
                    let jsonData = JSON.parse(res.data);
                    let dbData = {
                        key:res.data,
                        xpub:jsonData.xpub,
                        alias:alias,
                    };
                    let request = db.transaction(['keys'], 'readwrite')
                        .objectStore('keys')
                        .add(dbData);
                    request.onsuccess = function () {
                        resolve({xpub:jsonData.xpub, alias: alias});
                    };
                    request.onerror = function () {
                        reject(new Error('db insert error'));
                    };
                }).catch(error => {
                    reject(error);    
                });
            };
            getRequest.onerror = function () {
                reject(new Error('db get error'));
            };
        }).catch(error => {
            reject(error);
        });
    });
    return retPromise;
};


/**
 * Create a new key.
 *
 * @param {String} xpub - xpub.
 */
keysSDK.prototype.createPubkey = function(xpub) {
    let retPromise = new Promise((resolve, reject) => {
        let data = {};
        data.xpub = xpub;
        data.seed = 1;
        createPubkey(data).then((res) => {
            let jsonData = JSON.parse(res.data);
            resolve(jsonData);
        }).catch(error => {
            reject(error);
        });
    });
    return retPromise;
};

export default keysSDK;</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Apr 29 2019 10:37:11 GMT+0800 (CST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>



</body>
</html>
